import React from 'react'

import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'
import { HiddenControlSchema } from 'amis/lib/renderers/Form/Hidden'
import { TextControlSchema } from 'amis/lib/renderers/Form/InputText'
import { DateTimeControlSchema } from 'amis/lib/renderers/Form/InputDate'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import { DefaultCrudTableServerApi } from '@/components/DefaultCrudTable/types/DefaultCrudTableProps'
import amisUtils from '@/components/amis/amisUtils'
import Oauth2RegisteredClientClientAuthenticationMethodsEnum from '@/core/oauth2/enums/Oauth2RegisteredClientClientAuthenticationMethodsEnum'
import Oauth2RegisteredClientAuthorizationGrantTypesEnum from '@/core/oauth2/enums/Oauth2RegisteredClientAuthorizationGrantTypesEnum'
import { SelectControlSchema } from 'amis/lib/renderers/Form/Select'
import { EditorControlSchema } from 'amis/lib/renderers/Form/Editor'
import oauth2AdminApiConfig from '@/core/oauth2/generated/apiconfig/oauth2AdminApiConfig'

const Oauth2RegisteredClientList = () => {
	const entityName = '客户端'
	const isModifyModeExpression = '${isEditMode}'
	const modelApiConfig = oauth2AdminApiConfig.oauth2RegisteredClient
	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'id',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'clientId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.LIKE_RIGHT,
						propertyName: 'clientName',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'deleteFlag',
					} as RsqlDataOperator,
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			},
		},
		addServerApi: { apiConfig: modelApiConfig.add },
		deleteServerApi: {
			apiConfig: modelApiConfig.removeLogic,
		},
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${id}',
			},
		},
		editServerApi: {
			apiConfig: modelApiConfig.modifyNotNullField,
			data: {
				id: '${id}',
				clientSecret: '${clientSecret}',
				clientName: '${clientName}',
				clientAuthenticationMethods: '${clientAuthenticationMethods}',
				authorizationGrantTypes: '${authorizationGrantTypes}',
				redirectUris: '${redirectUris}',
				scopes: '${scopes}',
				clientSettings: '${clientSettings}',
				tokenSettings: '${tokenSettings}',
			},
		} as DefaultCrudTableServerApi,
		debug: false,
		formItems: [
			{
				type: 'hidden',
				name: 'id',
				label: 'ID',
			} as HiddenControlSchema,
			{
				type: 'input-text',
				name: 'clientName',
				label: '客户端名称',
				required: true,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'clientId',
				label: 'clientId',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'clientSecret',
				label: 'clientSecret',
				required: true,
			} as TextControlSchema,
			{
				type: 'input-datetime',
				name: 'clientSecretExpiresAt',
				label: '密钥过期时间',
				required: true,
			} as DateTimeControlSchema,
			{
				type: 'select',
				name: 'clientAuthenticationMethods',
				label: '客户端认证方式',
				clearable: true,
				multiple: true,
				required: true,
				options: amisUtils.getSelectOptionsFromBaseEnumValues(
					Oauth2RegisteredClientClientAuthenticationMethodsEnum.CLIENT_SECRET_BASIC,
					Oauth2RegisteredClientClientAuthenticationMethodsEnum.CLIENT_SECRET_POST,
					Oauth2RegisteredClientClientAuthenticationMethodsEnum.CLIENT_SECRET_JWT,
					Oauth2RegisteredClientClientAuthenticationMethodsEnum.PRIVATE_KEY_JWT,
					Oauth2RegisteredClientClientAuthenticationMethodsEnum.NONE,
				),
			} as SelectControlSchema,
			{
				type: 'select',
				name: 'authorizationGrantTypes',
				label: '授权协议',
				clearable: true,
				multiple: true,
				required: true,
				options: amisUtils.getSelectOptionsFromBaseEnumValues(
					Oauth2RegisteredClientAuthorizationGrantTypesEnum.CLIENT_CREDENTIALS,
					Oauth2RegisteredClientAuthorizationGrantTypesEnum.AUTHORIZATION_CODE,
					Oauth2RegisteredClientAuthorizationGrantTypesEnum.PASSWORD,
					Oauth2RegisteredClientAuthorizationGrantTypesEnum.REFRESH_TOKEN,
					Oauth2RegisteredClientAuthorizationGrantTypesEnum.REST,
					Oauth2RegisteredClientAuthorizationGrantTypesEnum.EXTERNAL_USER,
				),
			} as SelectControlSchema,
			{
				type: 'input-text',
				name: 'redirectUris',
				label: '重定向链接',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'scopes',
				label: 'scope',
				required: false,
				placeholder: '","分隔',
			} as TextControlSchema,
			{
				type: 'editor',
				name: 'clientSettings',
				label: '客户端配置(json)',
				size: 'xxl',
				allowFullscreen: false,
				options: {
					automaticLayout: true,
				},
				language: 'json',
				required: false,
			} as EditorControlSchema,
			{
				type: 'editor',
				name: 'tokenSettings',
				label: 'token配置(json)',
				size: 'xxl',
				allowFullscreen: false,
				options: {
					automaticLayout: true,
				},
				language: 'json',
				required: false,
			} as EditorControlSchema,
			{
				type: 'input-datetime',
				name: 'clientIdIssuedAt',
				label: '发布时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
			{
				type: 'input-datetime',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
			{
				type: 'input-datetime',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
		],
		columns: [
			{
				name: 'id',
				label: 'id',
				searchable: true,
			},
			{
				name: 'clientId',
				label: 'clientId',
				searchable: true,
			},
			{
				name: 'clientSecret',
				label: 'clientSecret',
			},
			{
				name: 'clientName',
				label: '客户端名称',
				searchable: true,
			},
			{
				name: 'clientAuthenticationMethods',
				label: '客户端认证方式',
			},
			{
				name: 'authorizationGrantTypes',
				label: '授权协议',
			},
			{
				name: 'redirectUris',
				label: '重定向链接',
			},
			{
				name: 'scopes',
				label: 'scope',
			},
			{
				name: 'clientSecretExpiresAt',
				label: '密钥过期时间',
			},
			{
				name: 'clientIdIssuedAt',
				label: '发布时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				name: 'clientSettings',
				label: '客户端配置',
			},
			{
				name: 'tokenSettings',
				label: 'token配置',
			},
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
		],
	})
	return <RealTimeCodeEditor code={schema} />
}

export default Oauth2RegisteredClientList
